<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>校验两次密码是否输入一致</title>
  </head>
  <body>
    <input type="text" name="password" />
    <input type="text" name="confirm_password" />
    <span name="msg"></span>

    <script>
      function query(name) {
        return document.querySelector(`[name="${name}"]`);
      }
      let inputs = document.querySelectorAll(
        '[name="password"],[name="confirm_password"]'
      );
      console.log(inputs);
      [...inputs].map((item) => {
        item.addEventListener("keyup", function () {
          let msg = "";
          if (
            query("password").value != query("confirm_password").value ||
            query("password").value.length < 5
          ) {
            msg = "两次密码输入不一致或密码长度小于5";
          }

          query("msg").innerHTML = msg;
        });
      });

      function star(num) {
        return "*".repeat(num || 5); // repeat重复输入
      }
      console.log(star(6));
    </script>
  </body>
</html>
